package org.itboys.shop.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.apache.ibatis.annotations.Param;
import org.itboys.baseobject.entity.City;
import org.itboys.baseobject.service.CityService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.tools.ResponseManager;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.product.dto.ReceiveAddressDTO;
import org.itboys.product.entity.ReceiveAddress;
import org.itboys.product.service.ReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/wx/address")
public class WXAddressController extends BaseController {

    @Autowired
    private ReceiveAddressService receiveAddressService;

    @Autowired
    private CityService cityService;

    //新增或者修改收货地址 asd
    @RequestMapping("/save")
    public void save(
    		@Param("addressId") Long addressId, 
    		@Param("provinceId") Long provinceId, 
    		@Param("cityId") Long cityId, 
    		@Param("districtId") Long districtId, 
    		@Param("address") String address,    
    		@Param("name") String name, 
    		@Param("mobile") String mobile, 
    		@Param("memberId") Long memberId, 
    		@Param("cityDetail") String cityDetail,
    		@Param("isDefault")Integer isDefault,
    		HttpServletResponse response, 
    		HttpServletRequest request, 
    		Model model) {
        try {
            Member member = (Member)request.getSession().getAttribute("user");
            receiveAddressService.save(addressId, provinceId, cityId, districtId, address, name, mobile, member.getId(), cityDetail,isDefault);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @RequestMapping("/detail")
    public void findInfo(@RequestParam long id,HttpServletResponse response, HttpServletRequest request){
        try{
            ReceiveAddressDTO address = receiveAddressService.findById(id);
            AjaxUtils.renderJson(response, address);
        } catch (Exception e) {
            logger.error(" ",e);
            ResponseManager.SystemError(response, null);
        }
    }

    //显示收货地址
    @RequestMapping("/show")
    public void address(HttpServletResponse response, HttpServletRequest request,Model model) {
        try {
        	Member member = (Member)request.getSession().getAttribute("user");
            Map<String, Object> param= Maps.newHashMap();
            param.put("memberId",member.getId());
            param.put("isDeleted in", new Integer[]{0,2});
            List<ReceiveAddress> addresses = receiveAddressService.list(param);
            List<ReceiveAddressDTO> addressResults = Lists.newArrayList();
            for (ReceiveAddress address : addresses) {
                ReceiveAddressDTO dto = new ReceiveAddressDTO(address.getId(), address.getMemberId(), address.getName(), address.getMobile(), address.getProvinceId(), address.getCityId(), address.getDistrictId(), address.getCityDetail(), address.getAddress(),address.getIsDeleted());
                addressResults.add(dto);
            }
            AjaxUtils.renderJson(response, addressResults);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseManager.SystemError(response, null);
        }
    }

    //删除收货地址
    @RequestMapping("/delete")
    public void deleteAddress(@RequestParam long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            receiveAddressService.deleteAddress(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }


    //设置默认地址
    @RequestMapping("/default")
    public void setDefault(@RequestParam long id, HttpServletRequest request, HttpServletResponse response) {
        try {
        	Member member = (Member)request.getSession().getAttribute("user");
        	receiveAddressService.setDefaultAddress(member.getId(), id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    //根据省份Id获得省份名称
    @RequestMapping("/cityDetail")
    public void getProvince(@RequestParam long id, HttpServletRequest request, HttpServletResponse response) {
        try {
            City city = cityService.getById(id);
            AjaxUtils.renderText(response, city.getMergerName());
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @RequestMapping("/toEdit")
    public ModelAndView toEdit(Long addressId,String callback,HttpServletResponse response, HttpServletRequest request, Model model) {
        try {
            if (null!=addressId) {
				ReceiveAddress address=receiveAddressService.getById(addressId);
				model.addAttribute("add", new ReceiveAddressDTO(address));
			}
            model.addAttribute("callback", callback);
            return new ModelAndView("/order/add_address");
        } catch (Exception e) {
            logger.error("",e);
            return null;
        }
    }

}
